# -*- coding: cp936 -*-
import os
import sys
import urllib2
import urllib
import xml.dom.minidom
import libxml2
import re

SET_DECKTOP = {}
SET_DECKTOP['url'] = 'http://dict.youdao.com/search'
SET_DECKTOP['params'] = {}
SET_DECKTOP['params']['le'] = 'eng'

SET_MOBILE = {}
SET_MOBILE['url'] = 'http://dict.youdao.com/dp/dp'
SET_MOBILE['params'] = {}
SET_MOBILE['params']['keyfrom'] = 'mdict.3.3.0.iphone'
SET_MOBILE['params']['version'] = 'iphone_3.0'

def dumpFile(fn, content):
    f = open(fn, 'wb')
    f.write(content.__str__())
    f.close()

def getBriefBlock(doc):
    root = doc.xpathEval2('//div[@id="phrsListTab"]')
    if len(root) == 0: return
    root = libxml2.htmlParseDoc(root[0].__str__(), 'utf-8')
    return root.__str__()
    
def getCollinsBlock(doc):
    root = doc.xpathEval2('//div[@id="collinsResult"]')
    if len(root) == 0: return
    root = libxml2.htmlParseDoc(root[0].__str__(), 'utf-8')

    part = root.xpathEval('//div[@class="wt-container"]')
    if len(part) == 0: return
    defs = getPart(part[0], 2)

    part_other = root.xpathEval('//div[@class="wt-container wt-collapse"]')
    if len(part_other):
        for part in part_other: defs += getPart(part, 1)
    return defs

def getPart(part, num):
    part = libxml2.htmlParseDoc(part.__str__(), 'utf-8')
    root = part.xpathEval('//h4')[0].parent
    defs = []
    """
    intro = root.xpathEval('./p[@class="collins-intro"]')
    if len(intro): defs.append(intro[0])
    """
    for li in root.xpathEval2('./ul')[-1].xpathEval('./li'):
        num -=1
        for p in li.xpathEval2('./p'):
            if len(p.xpathEval2('./@class')):continue
            c = p.children
            if c.name != 'span':
                if getText(c).strip() != '':continue
            else:
                if getText(c).strip() == 'PHRASAL VERB':continue
            item = [p]
            while True:
                div = p.nextElementSibling()
                if div and div.name == 'div':#example sentence
                    for s in div.xpathEval2('./p'):
                        item.append(s)
                        #break # comment this line, get example sentence without translation
                    break # uncomment this line, when you just wanna one sentence
                else:
                    break
            defs.append(item)
        if num == 0: break
    return map(getText, defs)

def getText(node):
    return "\n".join(map(getText, node)) if isinstance(node, list) else node.getContent()

def getWordPage(word, config=SET_DECKTOP):
    params = config['params']
    params['q'] = word
    url = config['url']+'?'+urllib.urlencode(params)
    result = urllib2.urlopen(url)
    if result.getcode() == 200 :
        content = deepStrip(result.read())
        dumpFile('word.html', content)
        doc = libxml2.htmlParseDoc(content, 'utf-8')
        getBriefBlock(doc)
        defs = getCollinsBlock(doc)
        print "%s - %d" % (word, len(defs))

def deepStrip(content):
    for r in re.compile('\s+', re.MULTILINE).findall(content): content = content.replace(r, ' ')
    for r in re.compile('\t+').findall(content): content = content.replace(r, ' ')
    for r in re.compile(' +').findall(content): content = content.replace(r, ' ')
    content = content.replace('> <', '><')
    r = re.compile('<span title="[^>]+>[^<]+</span>').finditer(content)
    try:
        count = 0
        while True:
            m = r.next()
            content = content[:count+m.end()] + ' ' + content[count+m.end():]
            count += 1
    except:
        None
    for r in re.compile(' +').findall(content): content = content.replace(r, ' ')
    return content

if __name__ == '__main__':
    getWordPage('carriage')
    getWordPage('whip')
    getWordPage('ring')
    getWordPage('take')
    getWordPage('fast')
